package org.istt.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.istt.domain.Settings;
import org.istt.service.SettingsManager;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SettingsAction extends ActionSupport {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final Log log = LogFactory.getLog(SettingsAction.class);
    private SettingsManager settingsManager;
	private String id;
	private String cancel;
	private Settings settings;
	public String edit() {
        // check for an add
		List<Settings> lstSettings = this.settingsManager.getList();
		if(lstSettings!=null && lstSettings.size()!=0){
			this.settings = lstSettings.get(0);
		}else{
			this.settings = new Settings();
			this.settings.setContactEmail("contact@abc.com");
			this.settings.setFooterMessage("footer message here");
			this.settings.setRecordPerPage(new Integer(10));
			this.settings.setSiteLogo("Site's logo");
			this.settings.setSiteName("Site name");
			this.settings.setSiteSologan("Sologan here");
		}
        return SUCCESS;
    }
	
	public String save(){
		if (log.isDebugEnabled()) log.debug("entering 'save' method");
        
		try {
			this.settingsManager.save(this.settings);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		// puts parameters to session
		ActionContext.getContext().getSession().put(WebConstants.FOOTER_MESSAGE, this.settings.getFooterMessage());
		ActionContext.getContext().getSession().put(WebConstants.NUMBER_RECORD_PER_PAGE, this.settings.getRecordPerPage());
		ActionContext.getContext().getSession().put(WebConstants.SITE_LOGO, this.settings.getSiteLogo());
		ActionContext.getContext().getSession().put(WebConstants.SITE_NAME, this.settings.getSiteName());
		ActionContext.getContext().getSession().put(WebConstants.SITE_SLOGAN, this.settings.getSiteSologan());
        
		List args = new ArrayList();
        ActionContext.getContext().getSession().put("message",getText("menu.msg.add", args));
		return SUCCESS;
	}

	public String execute() {
        if (cancel != null) {
            return "cancel";
        }
        return SUCCESS;
    }

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public void setSettingsManager(SettingsManager settingsManager) {
		this.settingsManager = settingsManager;
	}

	public SettingsManager getSettingsManager() {
		return settingsManager;
	}

	public void setSettings(Settings settings) {
		this.settings = settings;
	}

	public Settings getSettings() {
		return settings;
	}
}
